package com.ruitu.mall_98_1.product.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.beanu.arad.utils.ToastUtils;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.dao.api.APIRetrofit;
import com.ruitu.mall_98_1.product.adapter.ProductListAdapter;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.PageModel;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.product.Product;
import com.ruitu.router_module.bean.product.SearchCondition;
import com.ruitu.router_module.utils.RcvUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ProductListBySearchActivity extends BaseProListActivity {
    private SearchCondition condition;//传过来的搜索条件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_search.setText("");
        Bundle bundle = getIntent().getExtras();
        condition = (SearchCondition) bundle.getSerializable("condition");
        reqFirst();
        showProgress();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_product_list_by_model;
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        productAdapter = new ProductListAdapter(this);
        productAdapter.type = 2;
        return productAdapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.l_rcv_product);
    }

    @Override
    protected void onItemClick(View v, int pos) {
        Bundle bundle = new Bundle();
        Product p = (Product) productList.get(pos);
        bundle.putSerializable("product", p);
        startActivity(ProductDetailActivity.class, bundle);
    }

    //执行搜索商品
    private void reqSearchProResult() {
        APIRetrofit.getDefault().reqSearchProResult(condition.getProvinceId(),
                condition.getCityId(), condition.getCountyId(), condition.getClassA(),
                condition.getClassB(), condition.getClassC(), condition.getKeys(),
                condition.getCityName(), "0", condition.getGoodSaleModel(),//
                currPage, 20)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult<PageModel<Product>>>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                    }

                    @Override
                    public void onNext(BaseResult<PageModel<Product>> pageResult) {
                        hideProgress();
                        if (pageResult.isSuccess()) {
                            currPage = pageResult.getData().getCurrentPage();
                            totalPage = pageResult.getData().getTotalPage();
                            totalNum = pageResult.getData().getTotal();
                            List<Product> newData = pageResult.getData().getDataList();
                            productList.addAll(newData);
                            productAdapter.setDataList(productList);
                        } else {
                            ToastUtils.showShort(pageResult.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        productList.clear();
        reqSearchProResult();

        RcvUtils.refreshCompleteDelay(lRecyclerView, null);
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqSearchProResult();
        } else {
            refreshNoMoreData();
        }
    }

    public void onReqComlete(boolean isSuc, int type, Object data) {
        if (type == 1) {//商品列表请求完成

        }
    }
}
